let obj = {
    name: "iverson",
    foo: function (a, b) {
        console.log(this.name)
        console.log(a + b)
    }
}

let obj1 = {
    name: "kobe"
}


Function.prototype.myBind = function (context) {
    if (typeof this !== 'function') {
        throw new Error('出错')
    }
    let that = this
    var context = context || window
    let args = [...arguments].slice(1)
    return function F() {
        // 如果被new
        if (this instanceof F) {
        // if (this.__proto__ === F.prototype) {
            return new that(...arguments)
        } else {
            that.apply(context, [...args, ...arguments])
        }
    }
}

let bar = obj.foo.myBind(obj1, 1)
bar(2)
let baz = new bar(1, 2)
console.log(baz)


// let bar = obj.foo.bind(obj1)
// let baz = new bar()
// console.log(baz, "12")